<?php
class Hd_Template {
	private $Hd_Template_dir="templates/";
	private $Hd_Compile_dir='cache/';
	private $Hd_Left_Tag="{Hd:";
	private $Hd_Right_Tag="}";
	private $Hd_Is_suffix=TRUE;
	private $Hd_suffix="html";
	private $_tpl_vars=array();
	
	public function __construct($config = null) {
		if(!empty($config)){
			foreach($config as $key=>$row):
				eval('$this->'.$key.'="'.$row.'";');
			endforeach;
		}
	}
	
	public function assign($template_content,$content=null){
		if(is_array($template_content)){
			$this->_tpl_vars=array_merge($this->_tpl_vars,$template_content_array);
		}else{
			$this->_tpl_vars=array_merge($this->_tpl_vars,array("$template_content"=>"$content"));
		}
	}
	
	public function display($template_file){
		if($this->Hd_Is_suffix){
			$template_file=$template_file.".".$this->Hd_suffix;
		}
		$content=file_get_contents($this->Hd_Template_dir.$template_file);
		foreach($this->_tpl_vars as $key=>$row):
			$replace_key=$this->Hd_Left_Tag.$key.$this->Hd_Right_Tag;
			$replace_row='<?php echo "'.$row.'"; ?>';
			$content=str_replace("$replace_key","$replace_row",$content);
		endforeach;
		$newfilename=$this->Hd_Compile_dir.str_replace(strrchr($template_file, "."),"",$template_file).".php";
		file_put_contents($newfilename,$content);
		include $newfilename;
	}
}
?>